const user = {
  namespaced: true,
  state: {
    token: localStorage.getItem('token') || '',
    username: localStorage.getItem('username') || ''
  },
  getters: {
    token: state => state.token,
    username: state => state.username,
    isLoggedIn: state => state.token !== '' && state.username !== ''
  },
  mutations: {
    SET_TOKEN(state, { token, username }) {
      state.token = token
      state.username = username
      localStorage.setItem('token', token)
      localStorage.setItem('username', username)
    },
    CLEAR_TOKEN(state) {
      state.token = ''
      state.username = ''
      localStorage.removeItem('token')
      localStorage.removeItem('username')
    }
  },
  actions: {
    setToken({ commit }, { token, username }) {
      commit('SET_TOKEN', { token, username })
    },
    clearToken({ commit }) {
      commit('CLEAR_TOKEN')
    },
    resetToken({ commit }) {
      commit('CLEAR_TOKEN')
    }
  }
}

export default user